package net.huang.libManager.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.huang.libManager.domain.Manager;
import net.huang.libManager.service.ManagerService;

/**
 * 用户名和密码匹配判断
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 处理中文乱码
	    response.setContentType("text/html;charset=utf-8");
		// 获取用户名和密码
	    String username = request.getParameter("username");
	    String password = request.getParameter("username");
	   
	    // 得到所有管理员的用户名和密码
	    ManagerService service = new ManagerService();
	    List<Manager> list = service.getManagersIdAndPwd();
	    // 匹配用户名和密码
	    for (Manager m : list) {
	        if (m.getMid().equals(username) && m.getMpwd().equals(password)) {
	            request.getSession().setAttribute("manager", m);
	            // 将请求转发到IndexServlet，回显用户信息（用户名）
	            request.getRequestDispatcher("indexServlet").forward(request, response);
	            break;
	        }
	    }
		response.getWriter().print("用户名或密码输入错误，登录失败！！！");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
